define(function(require) {
	var $ = require("jq");
	function loadQRImage(url) {
		this.url = url;
		var _this = this;
		// 加载图片
		this.preLoadImg(this.url, function() {
			_this.insertImage();
			if(/MSIE 6.0/g.test(navigator.userAgent)){
				_this.IE6Scroll();
			};
		});
		//alert(navigator.userAgent);
	};
	loadQRImage.prototype = {
		preLoadImg : function(url, callBack) {
			var img = new Image();
			if (!!window.ActiveXObject) {
				img.onreadystatechange = function() {
					if (this.readyState == "complete") {
						callBack();
					};
				};
			} else {
				img.onload = function() {
					callBack();
				};
			};
			img.src = url;
		},
		IE6Scroll:function(){
			var sharePopup=$("#share-popup");
			var windowHeight=document.documentElement.clientHeight;
			var timer=null;
			$(window).scroll(function(){
				window.clearTimeout(timer);
				timer=window.setTimeout(function(){
					sharePopup.css("top",document.documentElement.scrollTop+windowHeight-180);
				},300);
			}).resize(function(){
				windowHeight=document.documentElement.clientHeight;
			});
		},
		insertImage : function() {
			$("<div id='share-popup' style='background:url("+this.url+") no-repeat 6px center;'><a id='shareBtn' href='#share'></a></div>").appendTo("body").fadeIn(2000);
			shareFun();
		}
	};
	// 创建一个二维码图片
	$(function(){
		if(/MSIE 6.0/g.test(navigator.userAgent)){
			var QR = new loadQRImage("/pinzhiV2.0/images/static/share-btn.gif");
		}else{
			var QR = new loadQRImage("/pinzhiV2.0/images/static/share-btn.png");
		};
	});
	
	var shareFun = function(){
		//分享成功！！！
	
				var shareList = "<div id='pzShare' class='share_content' style='display:none'>"+
			    " <h6><a href='javascript:' class='s_close'>关闭</a>立即分享<span>（分享成功后可以获得名望值哦~）</span></h6>"+
			     "<ul>"+
			     "  <li id='sinawb'><a href='javascript:' class='bds_tsina xlwb'>新浪微博</a></li>"+
			     "  <li id='qzone'><a href='javascript:' class='bds_qzone qqkj'>QQ空间</a></li>"+
			     " <li id='renren'><a href='javascript:' class='bds_renren rrw'>人人网</a></li>"+
			     " <li id='qqwb'><a href='javascript:' class='bds_tqq txwb'>腾讯微博</a></li>"+
			     "  <li id='kaixin'><a href='javascript:' class='bds_kaixin001 kxw'>开心网</a></li>"+
			    " </ul>"+
				"</div>";
			
			
				 var shareTypeId = $("#shareTypeId").val();
				 var productId = $("#productId").val();
				 var agentId = $("#agentId").val();
				$('body').append(shareList);
			
				//var MAP = [];
				$("#shareBtn").on("click",function(){
					var top = ($(window).height() - $("#pzShare").height())/2;
					var left = ($(window).width() - $("#pzShare").width())/2;
					var scrollTop = $(document).scrollTop();
					var scrollLeft = $(document).scrollLeft();
					if (top<0) {
						var top = 30;
					}
	
					$("#pzShare").fadeIn().css( { position : 'absolute', 'top' : top+scrollTop, left : left + scrollLeft } );;
	
					appendThick();
	
					$.getJSON("/crm/share/getShareInfo.do?callback=?",{"shareTypeId":shareTypeId,"productId":productId,"agentId":agentId},function(data){
						
							var  title = data.message;
			
							var  url =   encodeURIComponent(data.link);
			
							var  content = data.message;
							
							var  img = data.imgUrl;
							
							MAP = {
					   				sinawb : 'http://v.t.sina.com.cn/share/share.php?appkey=3421054943&title=' + content + '&url=' + url + '&source=bshare&retcode=0&pic='+img,
					   				qqwb : 'http://v.t.qq.com/share/share.php?title=' + content + '&site=' + url + '&pic='+img+'&url='+ url + '&appkey=801435572',
					   				renren : 'http://widget.renren.com/dialog/share?resourceUrl=' + url + '&title=' + '&images=&description=' + content+'image_src='+img,
					   				qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&title=' + '&pics='+img+'&summary=' + content,
					   				kaixin : 'http://www.kaixin001.com/rest/records.php?url='+url +'&content=' + content+'&pic='+img+'&aid=100013770&style=111'
					   		};
					});
				});
				
				$(".s_close").on("click",function(){
			
					$("#pzShare").hide();
					$('.thickdiv').remove();
			
				});
				
				$("#pzShare").find('li').on("click",function(){
					
			   	var url = MAP[$(this).attr('id')],
			   			top = $(window).height() > 400?($(window).height() - 400)/2 : 50,
			   			left = $(window).width() > 600?($(window).width() - 600)/2 : 50;
			   			window.open(url,'', 'height=400, width=600,left='+left+',top=' + top);
			   	
				   	var snsId =$(this).attr('id');
				   	$.getJSON("/crm/share/getFameFromShare.do?callback=?",{"cps":snsId,"objType":shareTypeId,"objId":productId},function(data){
				   			/*console.log(data);
				   			if (data.result=="1") {
				   				alert("分享成功！！！");
				   			};*/
				
				   	});
			   });
		//分享end
		//插入蒙层
		function appendThick(){
			var docu = $('body');
			var hei= docu.height();
			docu.append("<div class='thickdiv'></div>");
			$('.thickdiv').css("height",hei);
		}
	
	}
});